package com.googlecode.jtoy.mvc.action;

import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.googlecode.jtoy.mvc.service.LoginService;
import com.lowagie.text.pdf.BaseFont;

/**
 * @author shangxj
 * @date 9:39:14 PM, Jul 28, 2011
 */
@Controller
public class LoginController {

	@Autowired
	private LoginService loginService;

	@RequestMapping("listAllUsers")
	public void listAllUsers(ModelMap map) {

		map.put("users", loginService.listAllUsers());
	}

	@RequestMapping("test")
	public void viewPdf(String url, HttpServletResponse response) {
		try {
			response.setContentType("application/x-pdf ;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			response.setHeader("content-disposition", "attachment; filename=test.pdf");

			OutputStream os = response.getOutputStream();
			ITextRenderer renderer = new ITextRenderer();
			ITextFontResolver fontResolver = renderer.getFontResolver();
			fontResolver.addFont("/opt/workspace/stnc-oss/WebRoot/font/simsun.ttc", BaseFont.IDENTITY_H,
					BaseFont.NOT_EMBEDDED);
			renderer.setDocument(url);
			renderer.getSharedContext().setBaseURL("file:/opt/workspace/jtoy/WebRoot/");
			renderer.layout();
			renderer.createPDF(os);

			os.flush();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

}
